/*
 *                  BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 * 
 * Created on Nov 2, 2007
 * 
 */

package org.biojava.services.das.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class NewsDAO {

	public NewsDAO (){
		
	}
	
	public List<NewsItem> getAllNews(Connection conn) throws SQLException{
		String sql = "select * from news where news_id order by time desc";
		PreparedStatement ps = conn.prepareStatement(sql);
		
		
		List<NewsItem> newsList = new ArrayList<NewsItem>();
		ResultSet set = ps.executeQuery();
		
		while (set.next()){
			NewsItem news = new NewsItem();
			news.setTitle(set.getString("title"));
			news.setBody(set.getString("body"));
			java.sql.Timestamp t = set.getTimestamp("time");
			Date dat = new Date(t.getTime());
			news.setTime(dat);		
			newsList.add(news);
		}
		
		return newsList;
	}
	
	public NewsItem getNewsByID(Connection conn, int newsID) throws SQLException{
		String sql = "select * from news where news_id = ?";
		PreparedStatement ps = conn.prepareStatement(sql);
		
		ps.setInt(1,newsID);
		
		ResultSet set = ps.executeQuery();
		NewsItem news = new NewsItem();
		while (set.next()){
			news.setTitle(set.getString("title"));
			news.setBody(set.getString("body"));
			java.sql.Timestamp t = set.getTimestamp("time");
			Date dat = new Date(t.getTime());
			news.setTime(dat);		
	
		}
		return news;
	}
	
	
	
}
